package com.zzt.zt_gson;


import android.util.Log;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

public abstract class HttpCallback<T> {
    private static final String TAG = "Gson callback";

    public abstract void onResponse(HttpResponse<T> response);

    public final void onResponse(String resString) {
        Type type = getClass().getGenericSuperclass();

        Log.i(TAG, "type 1:" + type.toString());
        if (type instanceof ParameterizedType) {
            //ParameterizedType参数化类型，即泛型
            ParameterizedType typePar = (ParameterizedType) type;
            type = typePar.getActualTypeArguments()[0];
            Log.w(TAG, "type 2:" + type);
        } else {
            type = Object.class;
        }
        HttpResponse<T> resObj = (HttpResponse<T>) HttpResponse.fromJson(resString, type);
        onResponse(resObj);
    }

}
